package ui.gui;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import domein.DomeinController;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

public class KiesSpelbord extends JFrame implements ListSelectionListener, ActionListener {

	private DefaultListModel lstModel;
	private JList lstSpelborden;
	private ResourceBundle resourceBundle;
	
	private JButton cmdBevestig, cmdCancel, cmdNieuw, cmdVerwijder;
	
	private JLabel lblInfo;
	
	private String[][] spelborden;
	
	public KiesSpelbord(ResourceBundle resourceBundle) {
		super("InfectedZone - " + resourceBundle.getString("kiesSpelbord"));
		this.setLayout(new BorderLayout());
		
		this.resourceBundle = resourceBundle;
		
		this.spelborden = DomeinController.getInstance().geefSpelborden();
		
		DomeinController.getInstance().selecteerSpelbord(spelborden[0][0]);
		
		this.initComponents();
		
		this.addComponents();
		
		this.changeInfoText(0);
		
		this.setSize(450, 450);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setVisible(true);
	}
	
	private void initComponents() {	
		lblInfo = new JLabel("");
		
		this.lstModel = new DefaultListModel();
		
		this.vulLijst();
		
		lstSpelborden = new JList(this.lstModel);
		lstSpelborden.addListSelectionListener(this);
		lstSpelborden.setSelectedIndex(0);
		
		cmdBevestig = new JButton(resourceBundle.getString("bevestig"));
		cmdBevestig.addActionListener(this);
		cmdCancel = new JButton(resourceBundle.getString("annuleren"));
		cmdCancel.addActionListener(this);
		
		cmdNieuw = new JButton(resourceBundle.getString("nieuw"));
		cmdNieuw.addActionListener(this);
		cmdVerwijder = new JButton(resourceBundle.getString("verwijder"));
		cmdVerwijder.addActionListener(this);
	}
	
	private void addComponents() {		
		JPanel pnlRightButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		pnlRightButtons.add(cmdBevestig);
		pnlRightButtons.add(cmdCancel);
		
		JPanel pnlLeftButtons = new JPanel(new FlowLayout(FlowLayout.LEFT));
		pnlLeftButtons.add(cmdNieuw);
		pnlLeftButtons.add(cmdVerwijder);
		
		JPanel pnlRight = new JPanel(new BorderLayout());
		pnlRight.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
		pnlRight.add(lblInfo, BorderLayout.NORTH);
		pnlRight.add(pnlRightButtons, BorderLayout.SOUTH);
		
		JPanel pnlLeft = new JPanel(new BorderLayout());
		pnlLeft.add(new JScrollPane(this.lstSpelborden), BorderLayout.CENTER);
		pnlLeft.add(pnlLeftButtons, BorderLayout.SOUTH);
		
		JPanel pnlCenter = new JPanel(new GridLayout(1, 2));
		pnlCenter.add(pnlLeft);
		pnlCenter.add(pnlRight);
		
		add(pnlCenter, BorderLayout.CENTER);
	}
	
	private void changeInfoText(int index) {
		String[][] spelbord = DomeinController.getInstance().geefSpelbord();
		
		// Obstakels
		int muren = 0;
		int bomen = 0;
		int water = 0;
		
		// vijanden
		int krijgers = 0;
		int tovenaars = 0;
		int schurken = 0;
		int priesters = 0;
		
		for(int i=0; i<spelbord.length; i++) {
			for(int j=0; j<spelbord[i].length; j++) {
				if(spelbord[i][j].equals("+")) {
					muren++;
				}
				else if(spelbord[i][j].equals("*")) {
					bomen++;
				}
				else if(spelbord[i][j].equals("O")) {
					water++;
				}
				else if(spelbord[i][j].equals("P")) {
					priesters++;
				}
				else if(spelbord[i][j].equals("K")) {
					krijgers++;
				}
				else if(spelbord[i][j].equals("T")) {
					tovenaars++;
				}
				else if(spelbord[i][j].equals("S")) {
					schurken++;
				}
			}
		}
		
		String html = String.format("<html><h1>%s</h1>%s: %d<br />%s: %d</p><br /><br /><h3>%s (%d)</h3><p>%s: %d<br />%s: %d<br />%s: %d</p><br /><br /><h3>%s (%d)</h3><p>Krijgers: %d<br />Tovenaars: %d<br />Schurken: %d<br />Priesters: %d</p></html>", 
				this.spelborden[index][0], 
				resourceBundle.getString("breedte"),
				spelbord[0].length, 
				resourceBundle.getString("hoogte"),
				spelbord.length, 
				resourceBundle.getString("obstakels"),
				(muren+bomen+water), 
				resourceBundle.getString("aantalMuren"),
				muren, 
				resourceBundle.getString("aantalBomen"),
				bomen,
				resourceBundle.getString("aantalWater"),
				water, 
				resourceBundle.getString("vijanden"),
				(krijgers+tovenaars+schurken+priesters), krijgers, tovenaars, schurken, priesters);
		
		lblInfo.setText(html);
	}
	
	private void verwijderSpelbord() {
		if(!lstSpelborden.isSelectionEmpty()) {
			String[] options = {resourceBundle.getString("ja"), resourceBundle.getString("nee")};
			
			String verwijderBericht = resourceBundle.getString("verwijderSpelbordenBericht");
			
			verwijderBericht = verwijderBericht.replaceAll("%s", spelborden[lstSpelborden.getSelectedIndex()][0]);
			
			int keuze = JOptionPane.showOptionDialog(this, verwijderBericht, resourceBundle.getString("bevestig"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
		
			// -1 = kruisje		0 = Ja		1 = Nee
			if(keuze == 0) {
				int index = lstSpelborden.getSelectedIndex();
				
				DomeinController.getInstance().verwijderSpelbord(spelborden[lstSpelborden.getSelectedIndex()][0]);
				
				this.vulLijst();
				
				if(index == this.spelborden.length) {
					lstSpelborden.setSelectedIndex(index-1);
				}
				else {
					lstSpelborden.setSelectedIndex(index);
				}
			}
		}
	}
	
	public void vulLijst() {
		this.lstModel.clear();
		
		this.spelborden = DomeinController.getInstance().geefSpelborden();
		
		for(String[] e:spelborden) {
			lstModel.addElement(e[0]);
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if(e.getSource() == lstSpelborden && !lstSpelborden.isSelectionEmpty()) {
			int index = lstSpelborden.getSelectedIndex();
			
			DomeinController.getInstance().selecteerSpelbord(spelborden[index][0]);
			
			this.changeInfoText(index);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == cmdCancel) {
			System.exit(0);
		}
		else if(e.getSource() == cmdBevestig) {
			this.dispose();
			
			LevelEditor lvlEditor = new LevelEditor(this.resourceBundle);
			lvlEditor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
		else if(e.getSource() == cmdNieuw) {
			MaakNieuweMap maakNieuweMap = new MaakNieuweMap(this, this.resourceBundle);
			maakNieuweMap.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		}
		else if(e.getSource() == cmdVerwijder) {
			this.verwijderSpelbord();
		}
	}
}
